package com.cheer.controller;

import com.cheer.common.Result;
import com.cheer.enums.OaArticleClassEnum;
import com.cheer.enums.OaIntegralRuleEnum;
import com.cheer.service.AuthService;
import com.cheer.service.OaArticleClassService;
import com.cheer.service.OaArticleService;
import com.cheer.vo.OaArticleClassFrontVO;
import com.cheer.vo.OaArticleFrontVO;
import com.cheer.vo.OaArticleSimpleVO;
import com.cheer.vo.SimpleObjectVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author cheer
 */
@RestController
@RequestMapping("/article")
public class OaArticleController {

    @Autowired
    private OaArticleClassService oaArticleClassService;

    @Autowired
    private OaArticleService oaArticleService;

    @Autowired
    private AuthService authService;

    @ApiOperation("查看主页文章分类")
    @GetMapping("/main")
    public Result<List<SimpleObjectVO>> main(HttpSession session) {
        authService.getUserId(session);
        List<SimpleObjectVO> list = oaArticleClassService.listByLocation(OaArticleClassEnum.MAIN.getCode());
        return Result.success(list);
    }

    @ApiOperation("查看学习频道文章分类")
    @GetMapping("/study")
    public Result<List<SimpleObjectVO>> study(HttpSession session) {
        authService.getUserId(session);
        List<SimpleObjectVO> list = oaArticleClassService.listByLocation(OaArticleClassEnum.STUDY.getCode());
        return Result.success(list);
    }

    @ApiOperation("查看文章分类下的文章列表")
    @GetMapping("/list/class")
    public Result<OaArticleClassFrontVO> list(@RequestParam Integer id,
                                              HttpSession session) {
        authService.getUserId(session);
        OaArticleClassFrontVO articleClassFrontVO = oaArticleService.listByArticleClass(id);
        // 龙江之窗前面+5
        if(OaIntegralRuleEnum.ARTICLE_WIN_ID.equals(id)){
            List<OaArticleFrontVO> articleList = articleClassFrontVO.getArticleList();
            articleList.forEach( vo -> vo.setIntegral(5) );
        }
        return Result.success(articleClassFrontVO);
    }

    @ApiOperation("分页查看文章分类下的文章列表")
    @GetMapping("/list/class/page")
    public Result<List<OaArticleFrontVO>> listPage(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                   @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                   @RequestParam Integer id, HttpSession session) {
        authService.getUserId(session);
        List<OaArticleFrontVO> oaArticleFrontVOList = oaArticleService.listByArticleClass(pageNum, pageSize, id);
        return Result.success(oaArticleFrontVOList);
    }

    @ApiOperation("分页查看文章分类下的文章列表")
    @GetMapping("/list/recommend")
    public Result<List<OaArticleSimpleVO>> listRecommend(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                         @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                         HttpSession session) {
        authService.getUserId(session);
        List<OaArticleSimpleVO> oaArticleFrontVOList = oaArticleService.listRecommend(pageNum, pageSize);
        return Result.success(oaArticleFrontVOList);
    }

    @ApiOperation("进入文章")
    @GetMapping("/enter")
    public Result<String> enter(@RequestParam Integer id, HttpSession session) {
        return Result.success(oaArticleService.enter(id, authService.getUserId(session), authService.getOpenid(session)));
    }

}
